define([], function () {
    require.config({
    paths:{
        "mupreview":'../addons/mupreview/js/mupreview'
    },
    shim: {
        'mupreview': ['css!../addons/mupreview/css/mupreview.css']
    }
});
require(['table'], function (Table) {
    // console.log(Table.api.bindevent);
    //重构image实现
    $(document).on("click",'[data-tips-src]',function (){
        var has = function (strarray,str){
            var bol = false;
            for (var i in strarray){
                if(strarray[i]==str){
                    bol = true;
                    break;
                }
            }
            return bol;
        };
        var src = this.getAttribute('data-tips-src') || this.src;
        var strarr = src.split('.');
        var type = strarr[strarr.length-1];
        var imgeArray = [
            'png',
            'jpeg',
            'jpg',
            'gif'
        ];
        var bols = has(imgeArray,type);
        //图片处理
        if(bols){
            var img = new Image();
            var imgWidth = this.getAttribute('data-width') || '480px';
            img.onload = function () {
                var $content = $(img).appendTo('body').css({background: '#fff', width: imgWidth, height: 'auto'});
                Layer.open({
                    type: 1, area: imgWidth, title: false, closeBtn: 1,
                    skin: 'layui-layer-nobg', shadeClose: true, content: $content,
                    end: function () {
                        $(img).remove();
                    },
                    success: function () {

                    }
                });
            };
            img.onerror = function (e) {

            };
            img.src = src;
        }else{
            var mp4Array = [
                'mp4'
            ];
            bols = has(mp4Array,type);
            if(bols){
                var video = document.createElement("VIDEO");
                video.setAttribute("src",src);
                video.setAttribute("controls",'controls');

                var imgWidth = this.getAttribute('data-height') || '700';
                // console.log(imgWidth)
                var $content = $(video).appendTo('body').css({background: '#fff',width: (imgWidth-20)+'px',height: 'auth'});
                Layer.open({
                    type: 1, area: [imgWidth+'px',(imgWidth)+'px'], title: false, closeBtn: 1,
                    skin: 'layui-layer-nobg', shadeClose: true, content: $content,
                    offset:'100px',
                    end: function () {
                        $(video).remove();
                    },
                    success: function () {

                    }
                });
                // video.src = src;
            }else{
                // var html = '<div></div>';
                var imgWidth = this.getAttribute('data-height') || '700';
                // var $content = $(html).appendTo('body').css({background: '#fff',width: (imgWidth-20)+'px',height: 'auth'});
                // console.log(src)
                Layer.open({
                    type: 2, area: [imgWidth+'px',(imgWidth)+'px'], title: false, closeBtn: 1,
                    skin: 'layui-layer-nobg', shadeClose: true, content:Fast.api.cdnurl(src),
                    offset:'100px',
                    end: function () {
                        $(video).remove();
                    },
                    success: function () {

                    }
                });
            }
        }
    });
    Table.api.formatter.file = function (value){
        // console.log(value)
        var strarr = value.split('.');
        var type = strarr[strarr.length-1];
        var has = function (strarray,str){
            var bol = false;
            for (var i in strarray){
                if(strarray[i]==str){
                    bol = true;
                    break;
                }
            }
            return bol;
        };
        var imgageArray = [
            'png',
            'jpeg',
            'jpg',
            'gif'
        ];
        var bols = has(imgageArray,type);
        if(bols){
            return "<a href='javascript:void(0)' data-tips-image='"+value+"'><img src='"+value+"' class='img-sm img-center' ></a>";
        }
        return "<a href='javascript:void(0)' data-tips-src='"+value+"'><img src='"+Fast.api.fixurl('ajax/icon')+"?suffix="+type+"' class='images-center' ></a>";
        // return value;
        // return "<a href='javascript:void(0)' data-tips-image='"+value+"'><img src='"+value+"' class='img-sm img-center' ></a>";
    };
    Table.api.formatter.files = function (values){
        var imgageArray = [
            'png',
            'jpeg',
            'jpg',
            'gif'
        ];
        var has = function (strarray,str){
            var bol = false;
            for (var i in strarray){
                if(strarray[i]==str){
                    bol = true;
                    break;
                }
            }
            return bol;
        };
        var one = function (value){
            let strarr = value.split('.');
            let type = strarr[strarr.length-1];
            let bils = has(imgageArray,type);
            if(bils){
                return "<a href='javascript:void(0)' data-tips-image='"+value+"'><img src='"+value+"' class='img-sm img-center' ></a>";
            }
            return "<a href='javascript:void(0)' data-tips-src='"+value+"'><img src='"+Fast.api.fixurl('ajax/icon')+"?suffix="+type+"' class='img-sm images-center' ></a>";
        };
        var str = "";
        var valuesArray = values.split(',');
        for (var is in valuesArray){
            if(valuesArray[is]){
                str += one(valuesArray[is]);
            }
        }
        return str;
    };
    var _bindevent = Table.api.bindevent;
    Table.api.bindevent = function (table){
        _bindevent.apply(this, [table]);
        require(['mupreview'],function (){

        });
    };
});

require.config({
    paths: {
        'summernote': '../addons/summernote/lang/summernote-zh-CN.min'
    },
    shim: {
        'summernote': ['../addons/summernote/js/summernote.min', 'css!../addons/summernote/css/summernote.css'],
    }
});
require(['form', 'upload'], function (Form, Upload) {
    var _bindevent = Form.events.bindevent;
    Form.events.bindevent = function (form) {
        _bindevent.apply(this, [form]);
        try {
            //绑定summernote事件
            if ($(".summernote,.editor", form).size() > 0) {
                require(['summernote'], function () {
                    var imageButton = function (context) {
                        var ui = $.summernote.ui;
                        var button = ui.button({
                            contents: '<i class="fa fa-file-image-o"/>',
                            tooltip: __('Choose'),
                            click: function () {
                                parent.Fast.api.open("general.attachment/select?element_id=&multiple=true&mimetype=image/*", __('Choose'), {
                                    callback: function (data) {
                                        var urlArr = data.url.split(/\,/);
                                        $.each(urlArr, function () {
                                            var url = Fast.api.cdnurl(this);
                                            context.invoke('editor.insertImage', url);
                                        });
                                    }
                                });
                                return false;
                            }
                        });
                        return button.render();
                    };
                    var attachmentButton = function (context) {
                        var ui = $.summernote.ui;
                        var button = ui.button({
                            contents: '<i class="fa fa-file"/>',
                            tooltip: __('Choose'),
                            click: function () {
                                console.log("general.attachment/select?element_id=&multiple=true&mimetype=*")
                                parent.Fast.api.open("general.attachment/select?element_id=&multiple=true&mimetype=*", __('Choose'), {
                                    callback: function (data) {
                                        var urlArr = data.url.split(/\,/);
                                        $.each(urlArr, function () {
                                            var url = Fast.api.cdnurl(this);
                                            var node = $("<a href='" + url + "'>" + url + "</a>");
                                            context.invoke('insertNode', node[0]);
                                        });
                                    }
                                });
                                return false;
                            }
                        });
                        return button.render();
                    };

                    $(".summernote,.editor", form).summernote({
                        height: 250,
                        lang: 'zh-CN',
                        fontNames: [
                            'Arial', 'Arial Black', 'Serif', 'Sans', 'Courier',
                            'Courier New', 'Comic Sans MS', 'Helvetica', 'Impact', 'Lucida Grande',
                            "Open Sans", "Hiragino Sans GB", "Microsoft YaHei",
                            '微软雅黑', '宋体', '黑体', '仿宋', '楷体', '幼圆',
                        ],
                        fontNamesIgnoreCheck: [
                            "Open Sans", "Microsoft YaHei",
                            '微软雅黑', '宋体', '黑体', '仿宋', '楷体', '幼圆'
                        ],
                        toolbar: [
                            ['style', ['style', 'undo', 'redo']],
                            ['font', ['bold', 'underline', 'strikethrough', 'clear']],
                            ['fontname', ['color', 'fontname', 'fontsize']],
                            ['para', ['ul', 'ol', 'paragraph', 'height']],
                            ['table', ['table', 'hr']],
                            ['insert', ['link', 'picture', 'video']],
                            ['select', ['image', 'attachment']],
                            ['view', ['fullscreen', 'codeview', 'help']],
                        ],
                        buttons: {
                            image: imageButton,
                            attachment: attachmentButton,
                        },
                        dialogsInBody: true,
                        followingToolbar: false,
                        callbacks: {
                            onChange: function (contents) {
                                $(this).val(contents);
                                $(this).trigger('change');
                            },
                            onInit: function () {
                            },
                            onImageUpload: function (files) {
                                var that = this;
                                //依次上传图片
                                for (var i = 0; i < files.length; i++) {
                                    Upload.api.send(files[i], function (data) {
                                        var url = Fast.api.cdnurl(data.url);
                                        $(that).summernote("insertImage", url, 'filename');
                                    });
                                }
                            }
                        }
                    });
                });
            }
        } catch (e) {

        }

    };
});

});